package org.iteam.xlz.core.service.terminal;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.terminal.Terminal;

public class TerminalService extends BaseService<Terminal>{
	public static TerminalService instance;
	
	private TerminalService(){}
	
	public static TerminalService getInstance(){
		if(null==instance){
			instance=new TerminalService();
		}
		return instance;
	}
	
	public long counts(String aid1,String aid2,String aid3,String aid4){
		try {
			List<Object> params=new ArrayList<Object>();
			StringBuilder sql=new StringBuilder("select count(1) from terminal where 1=1");
			if(StringUtils.isNotBlank(aid4)){
				sql.append(" and aid=?");
				params.add(aid4);
			}else
				if(StringUtils.isNotBlank(aid3)){
					sql.append(" and aid in (select aid from area where 1=1 and pid=?)");
					params.add(aid3);
				}else
					if(StringUtils.isNotBlank(aid2)){
						sql.append(" and aid in (select aid from area where 1=1 and pid in (select aid from AREA where 1=1 and pid =?))");
						params.add(aid2);
					}else
						if(StringUtils.isNotBlank(aid1)){
							sql.append(" and aid in (select aid from area where 1=1 and pid in (select aid from AREA where 1=1 and pid in (select aid from AREA where 1=1 and pid =?)))");
							params.add(aid1);
						}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Terminal> query(String aid1,String aid2,String aid3,String aid4,long page,long rows){
		try {
			List<Object> params=new ArrayList<Object>();
			StringBuilder sql=new StringBuilder("select l.tid,l.community,r.typename as ttid,");
			sql.append("l.serialno,l.entertime,l.linkman,l.linktel,l.longitude,l.latitude,l.createtime,l.creator");
			sql.append(" from terminal l left join terminaltype r on l.ttid=r.ttid where 1=1");
			if(StringUtils.isNotBlank(aid4)){
				sql.append(" and l.aid=?");
				params.add(aid4);
			}else
				if(StringUtils.isNotBlank(aid3)){
					sql.append(" and l.aid in (select aid from area where 1=1 and pid=?)");
					params.add(aid3);
				}else
					if(StringUtils.isNotBlank(aid2)){
						sql.append(" and l.aid in (select aid from area where 1=1 and pid in (select aid from AREA where 1=1 and pid =?))");
						params.add(aid2);
					}else
						if(StringUtils.isNotBlank(aid1)){
							sql.append(" and l.aid in (select aid from area where 1=1 and pid in (select aid from AREA where 1=1 and pid in (select aid from AREA where 1=1 and pid =?)))");
							params.add(aid1);
						}
			sql.append(" order by l.createtime desc limit ?,?");
			params.add((page-1)*rows);
			params.add(rows);
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Terminal>();
		}
	}
	
	public List<Terminal> queryTerminals(String pid,String aid){
		try {
			return super.query("select * from terminal where 1=1 and aid=? and tid not in(select tid from productterminal  where pid=?)",aid,pid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Terminal>();
		}
	}
	
	public Terminal findById(String id){
		try {
			return super.show("select * from terminal where 1=1 and tid=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Terminal();
		}
	}
	
	public boolean update(Terminal model){
		SqlBuilder sql=SqlBuilder.createUpdate("terminal");

		sql.addField("ttid",model.getTtid());
		sql.addField("aid",model.getAid());
		sql.addField("serialno",model.getSerialno());
		sql.addField("entertime",model.getEntertime());
		sql.addField("linkman",model.getLinkman());
		sql.addField("linktel",model.getLinktel());
		sql.addField("longitude",model.getLongitude());
		sql.addField("latitude",model.getLatitude());
		sql.addField("community",model.getCommunity());
		sql.addWhere("tid",model.getTid());
		
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean create(Terminal model){
		SqlBuilder sql=SqlBuilder.createInsert("terminal");

		sql.addField("ttid",model.getTtid());
		sql.addField("aid",model.getAid());
		sql.addField("serialno",model.getSerialno());
		sql.addField("entertime",model.getEntertime());
		sql.addField("linkman",model.getLinkman());
		sql.addField("linktel",model.getLinktel());
		sql.addField("longitude",model.getLongitude());
		sql.addField("latitude",model.getLatitude());
		sql.addField("community",model.getCommunity());
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("tid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
